package com.jt.www.admin.stats.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.jt.www.admin.stats.service.InsuranceDataService;
import com.jt.www.model.Insurancedata.*;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.remote.StatisticClient;
import com.jt.www.util.JsonUtil;
import com.jt.www.util.MathUtil;
import com.jt.www.util.insurancedata.ExcelUtil;
import com.jt.www.util.insurancedata.InsuranceDataEnum;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

@Service
public class InsuranceDataServiceImpl implements InsuranceDataService {

    public static final Logger log = LoggerFactory.getLogger(InsuranceDataServiceImpl.class);
    private static final DecimalFormat DF = new DecimalFormat("0.00%");
    /**
     * 汇总表 实体类需要参与合计的属性变量名
     */
    private static final List<String> TOTAL_STATUS_VARS = Arrays.asList("totalComm", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * 统保示范项目 实体类需要参与合计的属性变量名
     */
    private static final List<String> UNIT_TOTAL_VARS = Arrays.asList("monthCount", "lastCount", "totalCount", "curCount", "yearCount", "lastYearCount", "szTotalPeople", "szCurPeople", "szYearPeople", "xzTotalPeople", "xzCurPeople", "xzYearPeople", "spTotalPeople", "spCurPeople", "spYearPeople", "yearPeople", "lastPeople", "lastPrem", "totalPrem", "curPrem", "curPrem", "yearPrem", "yearComm", "lastMonthPrem", "increaseRate", "monthTotalPrem", "monthPrem", "monthComm", "monthIndicator", "monthRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * 汇总表 实体类需要参与合计的属性变量名
     */
    private static final List<String> PART_TOTAL_VARS = Arrays.asList("totalPrem", "curPrem", "yearPrem", "yearComm", "yearConsual", "yearIncome", "lastComm", "monthTotalPrem", "monthPrem", "monthComm", "monthIndicator", "monthRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * ExtendVO 延伸业务 实体类需要参与合计的属性变量名
     */
    private static final List<String> EXTEND_VARS = Arrays.asList("xsTotalPrem", "xsCurPrem", "xsYearPrem", "xsYearComm", "xsLastPrem", "increaseRate", "jzTotalPrem", "jzCurPrem", "jzYearPrem", "jzYearComm", "sxTotalPrem", "sxCurPrem", "sxYearPrem", "sxYearComm", "orTotalPrem", "orCurPrem", "orYearPrem", "orYearComm", "monthTotalPrem", "monthPrem", "monthComm", "monthIndicator", "monthRate", "yearComm", "lastComm", "yearIncreaseRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * 我学保 实体类需要参与合计的属性变量名
     */
    private static final List<String> WXB_VARS = Arrays.asList("lastConfirmPrem", "ygTotalPrem", "ygCurPrem", "ygYearPrem", "ygYearComm", "azTotalPrem", "azCurPrem", "azYearPrem", "azYearComm", "bwTotalPrem", "bwCurPrem", "bwYearPrem", "bwYearComm", "ygMonthPrem", "ygMonthTotalPrem", "ygComm", "azMonthPrem", "azMonthTotalPrem", "azComm", "bwMonthPrem", "bwMonthTotalPrem", "bwComm", "comm", "monthIndicator", "monthRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * 学平险 实体类需要参与合计的属性变量名
     */
    private static final List<String> XP_VARS = Arrays.asList("totalPrem", "curPrem", "yearPrem", "yearComm", "lastComm", "monthPrem", "monthTotalPrem", "monthComm", "monthIndicator", "monthRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * 校车业务 实体类需要参与合计的属性变量名
     */
    private static final List<String> SCHOOL_BUS_VARS = Arrays.asList("totalPrem", "curPrem", "yearPrem", "yearComm", "lastComm", "monthPrem", "monthTotalPrem", "monthComm", "monthIndicator", "monthRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    /**
     * 安全咨询服务 实体类需要参与合计的属性变量名
     */
    private static final List<String> CONSULT_VARS = Arrays.asList("totalPrem", "curPrem", "yearPrem", "yearComm", "lastComm", "monthPrem", "monthTotalPrem", "monthComm", "monthIndicator", "monthRate", "totalIndicator", "totalRate", "yearIndicator", "yearRate");
    @Autowired
    private StatisticClient statisticClient;

    /**
     * 业务同比情况表
     *
     * @param dataApiBean
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<BusinessGrewVO> getBusinessDataList(DataApiBean dataApiBean) throws Exception {
        //同比数据类型 1-新增注册、2-新增投保、3-新增保费、4-新增人数，5-保费
        GenericDataResponse<List<BusinessGrewVO>> result = null;
        String queryType = dataApiBean.getQueryType();
        switch (queryType) {
            case "1"://新增注册
                result = statisticClient.getNewgetRegistered(dataApiBean);
                break;
            case "2"://新增投保
                result = statisticClient.getNewInsurePrem(dataApiBean);
                break;
            case "3"://新增保费
                result = statisticClient.getNewInsurePrem(dataApiBean);
                break;
            case "4"://新增人数
                result = statisticClient.getNewInsurePeople(dataApiBean);
                break;
            case "5"://保费
                result = statisticClient.getNewPrem(dataApiBean);
                break;
            default:
                log.error("QueryType=[1-新增注册、2-新增投保、3-新增保费、4-新增人数，5-保费]，前端入参={}", queryType);
                throw new Exception("同比项类型有误");
        }
        MyPageInfo<BusinessGrewVO> pageInfo = new MyPageInfo<>();
        if (result != null) {

            if (StringUtils.equals(result.getCode(), "0000")) {
                List<BusinessGrewVO> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    for (BusinessGrewVO businessGrewVO : list) {
                        BigDecimal newCount = MathUtil.getBigDecimal(businessGrewVO.getNewCount());
                        BigDecimal lastCount = MathUtil.getBigDecimal(businessGrewVO.getLastCount());
                        //同比 计算公式为 (本期-同期)/同期*100%，保留2位小数
                        BigDecimal compareCount = (newCount.subtract(lastCount)).divide(newCount, 4, BigDecimal.ROUND_HALF_UP);
                        if (compareCount.compareTo(BigDecimal.ZERO) == 0) {
                            businessGrewVO.setCompareCount("0");
                        } else {
                            businessGrewVO.setCompareCount(DF.format(compareCount.toString()));
                        }

                    }
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }
        return pageInfo;
    }

    /**
     * 统保示范项目业务统计
     *
     * @param dataApiBean
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<UnfiedInsureProjectV0> getUnfiedInsureProject(DataApiBean dataApiBean) throws Exception {
        GenericDataResponse<List<UnfiedInsureProjectV0>> result = statisticClient.getUnfiedInsureProject(dataApiBean);
        MyPageInfo<UnfiedInsureProjectV0> pageInfo = new MyPageInfo<>();
        if (result != null) {
            if (StringUtils.equals(result.getCode(), "0000")) {
                List<UnfiedInsureProjectV0> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }

        return pageInfo;
    }

    /**
     * 统保附加险投保率统计
     *
     * @param dataApiBean
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<AdditionInsureRateV0> getAdditionInsureRate(DataApiBean dataApiBean) throws Exception {
        GenericDataResponse<List<AdditionInsureRateV0>> result = statisticClient.getAdditionInsureRate(dataApiBean);
        MyPageInfo<AdditionInsureRateV0> pageInfo = new MyPageInfo<>();
        if (result != null) {
            if (StringUtils.equals(result.getCode(), "0000")) {
                List<AdditionInsureRateV0> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    BigDecimal totalCount;
                    BigDecimal totalPeople;
                    BigDecimal totalPrem;
                    BigDecimal count;
                    BigDecimal people;
                    BigDecimal prem;
                    BigDecimal countScale;
                    BigDecimal peopleScale;
                    BigDecimal premScale;
                    for (AdditionInsureRateV0 additionInsureRateV0 : list) {
                        totalCount = MathUtil.getBigDecimal(additionInsureRateV0.getTotalCount());
                        totalPeople = MathUtil.getBigDecimal(additionInsureRateV0.getTotalPeople());
                        totalPrem = MathUtil.getBigDecimal(additionInsureRateV0.getTotalPrem());
                        count = MathUtil.getBigDecimal(additionInsureRateV0.getCount());
                        people = MathUtil.getBigDecimal(additionInsureRateV0.getPeople());
                        prem = MathUtil.getBigDecimal(additionInsureRateV0.getPrem());
                        //同比 保留2位小数
                        if (totalCount.compareTo(BigDecimal.ZERO) != 0) {
                            countScale = count.divide(totalCount, 4, BigDecimal.ROUND_HALF_UP);
                            if (countScale.compareTo(BigDecimal.ZERO) == 0) {
                                additionInsureRateV0.setCountScale("0");
                            } else {
                                additionInsureRateV0.setCountScale(DF.format(countScale.toString()));
                            }
                        } else {
                            additionInsureRateV0.setCountScale("0");
                        }

                        if (totalPeople.compareTo(BigDecimal.ZERO) != 0) {
                            peopleScale = people.divide(totalPeople, 4, BigDecimal.ROUND_HALF_UP);
                            if (peopleScale.compareTo(BigDecimal.ZERO) == 0) {
                                additionInsureRateV0.setPeopleScale("0");
                            } else {
                                additionInsureRateV0.setPeopleScale(DF.format(peopleScale.toString()));
                            }
                        } else {
                            additionInsureRateV0.setPeopleScale("0");
                        }

                        if (totalPrem.compareTo(BigDecimal.ZERO) != 0) {
                            premScale = prem.divide(totalPrem, 4, BigDecimal.ROUND_HALF_UP);
                            if (premScale.compareTo(BigDecimal.ZERO) == 0) {
                                additionInsureRateV0.setPremScale("0");
                            } else {
                                additionInsureRateV0.setPremScale(DF.format(premScale.toString()));
                            }
                        } else {
                            additionInsureRateV0.setPremScale("0");
                        }

                    }
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }
        return pageInfo;
    }

    /**
     * 云南专用投保
     *
     * @param dataApiBean
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<YunnanSpecialVO> getYunnanSpecial(DataApiBean dataApiBean) throws Exception {
        GenericDataResponse<List<YunnanSpecialVO>> result = statisticClient.getYunnanSpecial(dataApiBean);
        MyPageInfo<YunnanSpecialVO> pageInfo = new MyPageInfo<>();
        if (result != null) {
            if (StringUtils.equals(result.getCode(), "0000")) {
                List<YunnanSpecialVO> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }
        return pageInfo;
    }

    /**
     * 同期投保情况明细表
     *
     * @param dataApiBean
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<PeriodinsVO> getPeriodinsList(DataApiBean dataApiBean) throws Exception {
        GenericDataResponse<List<PeriodinsVO>> result = statisticClient.getSamePeriodInsure(dataApiBean);
        MyPageInfo<PeriodinsVO> pageInfo = new MyPageInfo<>();
        if (result != null) {
            if (StringUtils.equals(result.getCode(), "0000")) {
                List<PeriodinsVO> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }
        return pageInfo;
    }

    /**
     * 实责转化安责情况表
     *
     * @param dataApiBean
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<ConverVO> getConvertList(DataApiBean dataApiBean) throws Exception {
        GenericDataResponse<List<ConverVO>> result = statisticClient.getAnnualStatistics(dataApiBean);
        MyPageInfo<ConverVO> pageInfo = new MyPageInfo<>();
        if (result != null) {
            if (StringUtils.equals(result.getCode(), "0000")) {
                List<ConverVO> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }
        return pageInfo;
    }

    /**
     * 安责险统计表
     *
     * @param dataApiBean
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public MyPageInfo<SafeinsVO> getSafeinsList(DataApiBean dataApiBean) throws Exception {
        GenericDataResponse<List<SafeinsVO>> result = statisticClient.getSecurityInsurance(dataApiBean);
        MyPageInfo<SafeinsVO> pageInfo = new MyPageInfo<>();
        if (result != null) {
            if (StringUtils.equals(result.getCode(), "0000")) {
                List<SafeinsVO> list = result.getData();
                if (list != null && !list.isEmpty()) {
                    for (SafeinsVO safeinsVO : list) {
                        BigDecimal totalCount = MathUtil.getBigDecimal(safeinsVO.getTotalCount());
                        BigDecimal totalPrem = MathUtil.getBigDecimal(safeinsVO.getTotalPrem());
                        //同比 计算公式为 (本期-同期)/同期*100%，保留2位小数
                        if (totalCount.compareTo(BigDecimal.ZERO) != 0) {
                            BigDecimal eventPrem = totalPrem.divide(totalCount, 2, BigDecimal.ROUND_HALF_UP);
                            if (eventPrem.compareTo(BigDecimal.ZERO) == 0) {
                                safeinsVO.setEvenPrem("0");
                            } else {
                                safeinsVO.setEvenPrem(eventPrem.toString());
                            }
                        } else {
                            safeinsVO.setEvenPrem("0");
                        }
                    }
                    String total = list.get(0).getTotal();
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(list);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(Long.valueOf(total));
                } else {
                    pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
                    pageInfo.setList(null);
                    pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
                    pageInfo.setTotal(0L);
                }
            } else {
                log.error("msg={}", result.getMsg());
                throw new Exception(result.getMsg());
            }
        } else {
            pageInfo.setCurrentPage(Integer.valueOf(dataApiBean.getPn()));
            pageInfo.setList(null);
            pageInfo.setPageSize(Integer.valueOf(dataApiBean.getPs()));
            pageInfo.setTotal(0L);
        }
        return pageInfo;
    }

    /**
     * 汇总表
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getTotalStatus(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getTotalStatus", TotalVO.class);
    }

    /**
     * 区域统保
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getPartTotal(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getPartTotal", PartTotalVO.class);
    }

    /**
     * 统保示范项目
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getUnitTotal(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getUnitTotal", UnitTotalVO.class);
    }

    /**
     * 延伸业务
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getExtend(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getExtend", ExtendVO.class);
    }

    /**
     * 我学保
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getWoXueBao(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getWoXueBao", WoXueBaoVO.class);
    }

    /**
     * 学平险
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getXuePing(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getXuePing", XuePingVO.class);
    }

    /**
     * 校车业务
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getSchoolBus(DataApiBean dataApiBean) throws Exception {
        return getEduStatistics(dataApiBean, "getSchoolBus", SchoolBusVO.class);
    }

    /**
     * 安全咨询服务A
     *
     * @param dataApiBean
     */
    @Override
    public List<Object> getConsult(DataApiBean dataApiBean) throws Exception {

        return getEduStatistics(dataApiBean, "getConsult", ConsultVO.class);
    }

    /**
     * 获取统计数据  利用反射给不同统计对象赋值并计算合计
     *
     * @param dataApiBean 前端入参
     * @param type        被调用方法名
     * @param clazz       需要赋值的对象类
     * @return
     * @throws Exception
     */
    private List<Object> getEduStatistics(DataApiBean dataApiBean, String type, Class clazz) throws Exception {
        GenericDataResponse<List<Object>> result = null;
        if (dataApiBean != null) {
            if (StringUtils.isEmpty(dataApiBean.getBranchCode())) {
                dataApiBean.setBranchCode(null);
            }
            if (StringUtils.isEmpty(dataApiBean.getSelectDate())) {
                dataApiBean.setSelectDate("");
            }
        }
        List<String> varList = new ArrayList<>();
        //通过方法名调用不同的统计服务接口，并获取需要赋值的对象变量名
        switch (type) {
            case "getTotalStatus":
                result = statisticClient.getTotalStatus(dataApiBean);
                varList = TOTAL_STATUS_VARS;
                break;
            case "getUnitTotal":
                result = statisticClient.getUnitTotal(dataApiBean);
                varList = UNIT_TOTAL_VARS;
                break;
            case "getPartTotal":
                result = statisticClient.getPartTotal(dataApiBean);
                varList = PART_TOTAL_VARS;
                break;
            case "getExtend":
                result = statisticClient.getExtend(dataApiBean);
                varList = EXTEND_VARS;
                break;
            case "getWoXueBao":
                result = statisticClient.getWoXueBao(dataApiBean);
                varList = WXB_VARS;
                break;
            case "getXuePing":
                result = statisticClient.getXuePing(dataApiBean);
                varList = XP_VARS;
                break;
            case "getSchoolBus":
                result = statisticClient.getSchoolBus(dataApiBean);
                varList = SCHOOL_BUS_VARS;
                break;
            case "getConsult":
                result = statisticClient.getConsult(dataApiBean);
                varList = CONSULT_VARS;
                break;
        }
        if (result == null) {
            return null;
        }
        if (!StringUtils.equals(result.getCode(), "0000")) {
            log.error("msg={}", result.getMsg());
            throw new Exception(result.getMsg());
        }
        List<Object> list = result.getData();
        if (list == null || list.isEmpty()) {
            return null;
        }
        //计算合计
        //统计查询对应变量
        BigDecimal voNum;
        //合计对应变量
        BigDecimal num;
        Object o = clazz.newInstance();
        BeanUtils.setProperty(o, "branchCode", "JT");
        //返回一条数据 和 返回多条数据 分开处理分支机构属性
        if (list.size() == 1) {
            BeanUtils.setProperty(o, "branchName", BeanUtils.getProperty(list.get(0), "branchName"));
        } else {
            BeanUtils.setProperty(o, "branchName", "全国");
        }
        //反射调用set get 方法进行计算 并 赋值
        for (Object obj : list) {
            for (String var : varList) {
                try {
                    voNum = new BigDecimal(BeanUtils.getProperty(obj, var));
                } catch (NullPointerException e) {
                    voNum = BigDecimal.ZERO;
                }
                //计算合计  累加并赋值
                num = new BigDecimal(BeanUtils.getProperty(o, var));
                BeanUtils.setProperty(o, var, num.compareTo(BigDecimal.ZERO) == 0 ? voNum : num.add(voNum));
            }
        }
        list.add(o);
        return list;
    }

    /**
     * 获取统计数据并导出excel文件
     *
     * @param dataApiBean
     * @param type
     * @param response
     * @throws Exception
     */
    public void getDataByExcel(DataApiBean dataApiBean, String type, HttpServletResponse response) throws Exception {
        InsuranceDataEnum[] values = InsuranceDataEnum.values();
        for (InsuranceDataEnum value : values) {
            if (StringUtils.equals(type, value.getType())) {
                List<Object> eduStatistics = getEduStatistics(dataApiBean, value.getMethodName(), value.getClazz());
                if (eduStatistics == null || eduStatistics.isEmpty()) {
                    throw new Exception("空列表无法导出");
                }
                long timeInMillis = Calendar.getInstance().getTimeInMillis();
                /* ExcelUtil.exportExcel(eduStatistics,value.getTitle(),value.getTitle(), value.getClazz(),String.valueOf(timeInMillis),response);
                 break;*/
                String str = JsonUtil.object2Json(eduStatistics);
                /**
                 * Excel 导出时必须传入具体对象集合，不然无法解析数据，因此需要把 List<Object> 转成具体的对象集合
                 */
                switch (type) {
                    case "1":
                        List<TotalVO> totalVOS = JsonUtil.json2Object(str, new TypeReference<List<TotalVO>>() {
                        });
                        ExcelUtil.exportExcel(totalVOS, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    /**
                     * YPE1("1","getTotalStatus",TotalVO.class,"佣金完成情况","佣金完成情况"),
                     *     TYPE2("2","getPartTotal", PartTotalVO.class,"区域统保","区域统保"),
                     *     TYPE3("3","getUnitTotal", UnitTotalVO.class,"统保示范项目","统保示范项目"),
                     *     TYPE4("4","getExtend", ExtendVO.class,"延伸业务","延伸业务"),
                     *     TYPE5("5","getWoXueBao", WoXueBaoVO.class,"我学保","我学保"),
                     *     TYPE6("6","getXuePing",XuePingVO.class,"学平险","学平险"),
                     *     TYPE7("7","getSchoolBus",SchoolBusVO.class,"校车业务","校车业务"),
                     *     TYPE8("8","getConsult",ConsultVO.class,"安全咨询服务","安全咨询服务");
                     */
                    case "2":
                        List<PartTotalVO> partTotalVOs = JsonUtil.json2Object(str, new TypeReference<List<PartTotalVO>>() {
                        });
                        ExcelUtil.exportExcel(partTotalVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    case "3":
                        List<UnitTotalVO> unitTotalVOs = JsonUtil.json2Object(str, new TypeReference<List<UnitTotalVO>>() {
                        });
                        ExcelUtil.exportExcel(unitTotalVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    case "4":
                        List<ExtendVO> extendVOs = JsonUtil.json2Object(str, new TypeReference<List<ExtendVO>>() {
                        });
                        ExcelUtil.exportExcel(extendVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    case "5":
                        List<WoXueBaoVO> woXueBaoVOs = JsonUtil.json2Object(str, new TypeReference<List<WoXueBaoVO>>() {
                        });
                        ExcelUtil.exportExcel(woXueBaoVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    case "6":
                        List<XuePingVO> xuePingVOs = JsonUtil.json2Object(str, new TypeReference<List<XuePingVO>>() {
                        });
                        ExcelUtil.exportExcel(xuePingVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    case "7":
                        List<SchoolBusVO> schoolBusVOs = JsonUtil.json2Object(str, new TypeReference<List<SchoolBusVO>>() {
                        });
                        ExcelUtil.exportExcel(schoolBusVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                    case "8":
                        List<ConsultVO> consultVOs = JsonUtil.json2Object(str, new TypeReference<List<ConsultVO>>() {
                        });
                        ExcelUtil.exportExcel(consultVOs, value.getTitle(), value.getTitle(), value.getClazz(), String.valueOf(timeInMillis), response);
                        return;
                }
            }
        }
    }

    @Deprecated
    public void getDataByExcelv2(DataApiBean dataApiBean, String type, HttpServletResponse response) throws Exception {
        InsuranceDataEnum[] values = InsuranceDataEnum.values();
        List<Object> eduStatistics = null;
        InsuranceDataEnum IDE = null;
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        for (InsuranceDataEnum value : values) {
            if (StringUtils.equals(type, value.getType())) {
                IDE = value;
                eduStatistics = getEduStatistics(dataApiBean, value.getMethodName(), value.getClazz());
                if (eduStatistics == null || eduStatistics.isEmpty()) {
                    throw new Exception("空列表无法导出");
                }
                /*
                 break;*/
                //String str = JsonUtil.object2Json(eduStatistics);
            }
        }
        if (IDE != null) {
            ExcelUtil.exportExcel(eduStatistics, IDE.getTitle(), IDE.getTitle(), IDE.getClazz(), String.valueOf(timeInMillis), response);
        }
    }

}
